from flask import request, render_template, session
from database import get_db_connection
from datetime import datetime
from flask import request, render_template, session
from database import get_db_connection
from datetime import datetime
from word_routes import get_random_word

def register_learn_routes(app):
    @app.route('/learn_by_chinese', methods=['GET', 'POST'])
    def learn_by_chinese():
        if request.method == 'GET':
            word = get_random_word()
            if word:
                session['current_word'] = {
                    'id': word[0],
                    'chinese': word[1],
                    'hiragana': word[2],
                    'category': word[3],
                    'meaning': word[4]
                }
                return render_template('learn_by_chinese.html', prompt=session['current_word']['chinese'])
        elif request.method == 'POST':
            user_input = request.form.get('answer')
            correct_answer = session['current_word']['hiragana']
            is_correct = user_input == correct_answer
            if not is_correct:
                conn = get_db_connection()
                c = conn.cursor()
                c.execute('SELECT id, error_count FROM wrong_book WHERE word_id = ?', (session['current_word']['id'],))
                record = c.fetchone()
                if record:
                    c.execute('UPDATE wrong_book SET error_time = ?, error_count = ? WHERE id = ?', (datetime.now(), record[1] + 1, record[0]))
                else:
                    c.execute('INSERT INTO wrong_book (word_id, error_time, error_count) VALUES (?, ?, ?)', (session['current_word']['id'], datetime.now(), 1))
                conn.commit()
                conn.close()
            return render_template('learn_by_chinese.html', prompt=session['current_word']['chinese'], is_correct=is_correct, correct_answer=correct_answer)

    @app.route('/learn_by_meaning', methods=['GET', 'POST'])
    def learn_by_meaning():
        if request.method == 'GET':
            word = get_random_word()
            if word:
                session['current_word'] = {
                    'id': word[0],
                    'chinese': word[1],
                    'hiragana': word[2],
                    'category': word[3],
                    'meaning': word[4]
                }
                return render_template('learn_by_meaning.html', prompt=session['current_word']['meaning'])
        elif request.method == 'POST':
            user_input = request.form.get('answer')
            correct_answer = session['current_word']['hiragana']
            is_correct = user_input == correct_answer
            if not is_correct:
                conn = get_db_connection()
                c = conn.cursor()
                c.execute('SELECT id, error_count FROM wrong_book WHERE word_id = ?', (session['current_word']['id'],))
                record = c.fetchone()
                if record:
                    c.execute('UPDATE wrong_book SET error_time = ?, error_count = ? WHERE id = ?', (datetime.now(), record[1] + 1, record[0]))
                else:
                    c.execute('INSERT INTO wrong_book (word_id, error_time, error_count) VALUES (?, ?, ?)', (session['current_word']['id'], datetime.now(), 1))
                conn.commit()
                conn.close()
            return render_template('learn_by_meaning.html', prompt=session['current_word']['meaning'], is_correct=is_correct, correct_answer=correct_answer)